/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 * 
 *	Created on 2010-12-27 下午04:35:05
 */
package cn.com.ccxe.core.crawl.article;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ccxe.core.db.WebDB;
import cn.com.ccxe.core.db.WebWorkQueues;
import cn.com.ccxe.core.entity.ArticleLink;
import cn.com.ccxe.core.util.CodeUtil;

/**
 *
 * @author hetao
 */
public class ArticleDataUpdater extends Thread {
	
	//更新间隔
	private int 			updateInterval = 60*24*30;
	final Logger 			logger = LoggerFactory.getLogger(ArticleDataUpdater.class);
	final WebDB 			webDB	= WebDB.getInstance();
	final WebWorkQueues		queue = WebWorkQueues.getInstance();
	@Override
	public void run() {
		while(true) {
			process();
		}
	}
	public void process() {
		logger.info("{} 中有 {} 条数据.",webDB.getDatabaseName(),webDB.size());
		webDB.copy(queue, this);
		queue.sync();
		logger.info("{}更新后，有 {} 条数据。",queue.getDatabaseName(),queue.size());
//		try {
			//更新检测的周期为10分钟/次。
//			sleep(10*60*1000);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
	}
	
	public boolean checkUpdate(ArticleLink link) {
		int fetchTime = link.getFetchTime();
		if(fetchTime <= 0 ) {
//			logger.debug("从未采集过的文章链接 -> {}",link);
			return true;
		}
		int currentTime = CodeUtil.getDate();
		int updateTime = (currentTime - fetchTime);
		if( updateTime > updateInterval) {
//			logger.debug("到达更新时间，立即更新  -> {}",link);
			return true; //FIXME 更新调度
		} else {
//			logger.debug("还差 {} 分钟更新。",updateTime);
			return false;
		}
	}
	
}
